package com.test.springboot.mybatis.service.impl;

import com.test.springboot.mybatis.dao.mapper.PayRecordDetailMapper;
import com.test.springboot.mybatis.dao.mapper.PayRecordMapper;
import com.test.springboot.mybatis.entity.PayRecord;
import com.test.springboot.mybatis.entity.PayRecordDetail;
import com.test.springboot.mybatis.entity.PayRecordDetailExample;
import com.test.springboot.mybatis.entity.enums.EnumPayRecordStatus;
import com.test.springboot.mybatis.service.PayRecordDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 
 * @author cxb
 * @date 2018年8月8日
 */
@Slf4j
@Service
public class PayRecordDetailServiceImpl implements PayRecordDetailService {

    @Autowired
    private PayRecordDetailMapper payRecordRecordDetailMapper;

    @Autowired
    private PayRecordMapper payRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(PayRecordDetail detail) {
        if (null != detail) {
            int res = payRecordRecordDetailMapper.insert(detail);
            checkTotalMoney(detail.getPayRecordId());
            return 1 == res ? detail.getId() : 0;
        }
        return 0;
    }
    
    @Override
    public List<PayRecordDetail> getByRecordId(int recordId) {
        if (recordId > 0) {
            PayRecordDetailExample example = new PayRecordDetailExample();
            example.createCriteria().andPayRecordIdEqualTo(recordId);
            List<PayRecordDetail> res = payRecordRecordDetailMapper.selectByExample(example);
            return res;
        }
        return null;
    }

    @Override
    public PayRecordDetail getById(int id) {
        if (id > 0) {
            return payRecordRecordDetailMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(int id) {
        if (id > 0) {
            return payRecordRecordDetailMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(PayRecordDetail detail) {
        if (null != detail && detail.getId() > 0) {
            return payRecordRecordDetailMapper.updateByPrimaryKey(detail);
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(List<PayRecordDetail> detailList) {
        if (!CollectionUtils.isEmpty(detailList)) {
            payRecordRecordDetailMapper.batchInsert(detailList);
            checkTotalMoney(detailList.get(0).getPayRecordId());
        }
    }

    private void checkTotalMoney(int payRecordId) {
        PayRecordDetailExample example = new PayRecordDetailExample();
        example.createCriteria().andPayRecordIdEqualTo(payRecordId);
        List<PayRecordDetail> detailList = payRecordRecordDetailMapper.selectByExample(example);
        BigDecimal totalMoney = BigDecimal.ZERO;
        if (!CollectionUtils.isEmpty(detailList)) {
            for (PayRecordDetail pd : detailList) {
                totalMoney = totalMoney.add(pd.getMoney());
            }
            PayRecord record = this.payRecordMapper.selectByPrimaryKey(payRecordId);
            if (record != null) {
                if (record.getNeedMoney().compareTo(totalMoney) <= 0) {
                    record.setStatus(EnumPayRecordStatus.COMPLATE.getCode());
                    this.payRecordMapper.updateByPrimaryKey(record);
                }
            }
        }
    }

	@Override
    @Transactional(rollbackFor = Exception.class)
	public void delByPayRecordId(int payRecordId) {
		PayRecordDetailExample example = new PayRecordDetailExample();
		example.createCriteria().andPayRecordIdEqualTo(payRecordId);
		payRecordRecordDetailMapper.deleteByExample(example);
		
	}

}
